package com.lanou.controller;

import com.lanou.bean.Service;
import com.lanou.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务表
 */
@Controller
public class ServiceController {
    @Autowired
    private ServiceService serviceService;

//    查询所有数据
    @RequestMapping("/selectAllService")
    public String selectAllService(HttpServletRequest request){

      List<Service> serviceList=serviceService.selectAllService();
      System.out.println(serviceList);
      request.setAttribute("serviceList",serviceList);
       return "service/service_list";
    }

//  根据省份证号查询账务账号
    @RequestMapping("/selectAccountIdByIdcard")
    public String selectAccountIdByIdcard(String idcardNo,HttpServletRequest request){
//       根据身份证号获取账务id
        System.out.println(idcardNo+"接收前端的idcard");
        Integer account_id=  serviceService.selectAccountIdByIdcard(idcardNo);
        System.out.println(account_id);
        request.setAttribute("idcardNo",idcardNo);
        request.setAttribute("accountId",account_id);

//       获取cost.name
        List<String> nameList=serviceService.selectAllCostName();
        System.out.println(nameList);
        request.setAttribute("nameList",nameList);


        return "service/service_add";
    }

    @RequestMapping("/insert")
    public String insert(String name,Integer account_id,Service service){

    //      根据name获取cost.id
        System.out.println(name);
        Integer cost_id= serviceService.selectCostIdByName(name);
        System.out.println(cost_id);

//      插入数据

        int result= serviceService.insertService(service,account_id,cost_id);
        System.out.println(result+"插入成功");

        return "service/service_list";
    }

//  详细信息的回显
    @RequestMapping("/selectServiceById")
    public String selectServiceById(Integer service_id,HttpServletRequest request){
        System.out.println(service_id);
        Service service=serviceService.selectServiceById(service_id);
        request.setAttribute("service",service);
        System.out.println(service);
        return "service/service_detail";
    }

//    修改信息
    @RequestMapping("/updateServiceById")
    public String updateServiceById(Integer service_id,HttpServletRequest request){
        System.out.println(service_id);
        //       获取cost.name
        List<String> nameList=serviceService.selectAllCostName();
        System.out.println(nameList);
        request.setAttribute("nameList",nameList);

        Service service=serviceService.selectServiceById(service_id);
        request.setAttribute("service",service);
        System.out.println(service);

        return "service/service_modi";
    }
    @RequestMapping("/savaService")
    public String savaService(Integer service_id,String unix_host,String os_username){
        System.out.println(service_id+unix_host+os_username);
       int result=serviceService.updateServiceById(service_id,unix_host,os_username);
        System.out.println(result+"修改保存成功");
        return "service/service_list";
    }

    @RequestMapping("/updateService")
    public String updateService(Integer service_id){
        System.out.println(service_id);
        int result=serviceService.updateService(service_id,new Date());
        System.out.println(result+"修改状态成功");
        return "service/service_list";
    }

//    暂停开启状态改变

    @RequestMapping("/updateServiceStatus")
    public String updateServiceStatus(Integer service_id,String status){
        System.out.println(status);

        if(status.equals("1")){
            System.out.println(status);
            System.out.println(service_id);
            Date pause_date=new Date();
            System.out.println(pause_date);
            int result=serviceService.updateServiceStatus(service_id,pause_date);
            System.out.println(result);
            return "service/service_list";
       }else {
            System.out.println(status);
            int result=serviceService.updateServiceStatus1(service_id);
            System.out.println(result);
            return "service/service_list";
        }
    }
}
